﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AESUtil
{
    /// <summary>
    /// HistoryDialog.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryDialog : Window
    {
        public HistoryDialog()
        {
            InitializeComponent();
            this.freshButtonClick(null,null);
        }

        public void freshButtonClick(object sender, RoutedEventArgs e)
        {
            List<CryptItem> list = HistoryUtil.getHistory();
            this.historyList.ItemsSource = list;
        }

        private void historyList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CryptItem item = (CryptItem)this.historyList.SelectedItem;
            if(item != null)
            {
                this.inputBox.Text = item.CryptText;
                this.resultBox.Text = item.PlainText;
            }
            else
            {
                this.inputBox.Text = "";
                this.resultBox.Text = "";
            }

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
        public void ForceCloseButton_Click()
        {
            this.Closing -= Window_Closing; // 移除事件处理程序
            this.Close(); // 现在调用 Close() 不会再触发 MainWindow_Closing
        }

        private void clearButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = System.Windows.MessageBox.Show("确认要清除所有历史记录么？", "确认删除", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.OK) {
                HistoryUtil.clearHistory();
                this.freshButtonClick(sender, e);
            }
        }

        private void editButtonClick(object sender, RoutedEventArgs e)
        {
            CryptItem item = (CryptItem)this.historyList.SelectedItem;
            if (item != null)
            {
                SetHistoryName shn = new SetHistoryName();
                shn.name = item.Name;
                if (shn.ShowDialog() == true)
                {
                    item.Name = shn.name;
                    HistoryUtil.updateHistory((List<CryptItem>)historyList.ItemsSource);
                    this.freshButtonClick(null,null);
                }
            }
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            //this.freshButtonClick(sender, null);
        }

        private void delButtonClick(object sender, RoutedEventArgs e)
        {
            CryptItem item = (CryptItem)this.historyList.SelectedItem;
            if (item != null)
            {
                MessageBoxResult result = System.Windows.MessageBox.Show("确认要清除选中的历史记录么？", "确认删除", MessageBoxButton.OKCancel, MessageBoxImage.Question);
                if (result == MessageBoxResult.OK)
                {
                    List<CryptItem> list = (List<CryptItem>)historyList.ItemsSource;
                    list.Remove(item);
                    HistoryUtil.updateHistory(list);
                    this.freshButtonClick(null, null);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.freshButtonClick(sender, null);
        }
    }
}
